/*
 * Copyright (c) 2006-2022, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-04-13     RT-Thread    first version
 */

#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>

#include <arpa/inet.h>         /* 包含 ip_addr_t 等地址相关的头文件 */
#include <netdev.h>            /* 包含全部的 netdev 相关操作接口函数 */
#include <ntp.h>



#define DBG_TAG "main"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>

struct netdev *netdev_get_by_name(const char *name);
//#define netdev_is_internet_up(netdev)

extern int mount_init(void);
//extern void oled_display(void);
/* defined the LED0 pin: PB1 */
#define LED0_PIN    GET_PIN(B, 1)
int main(void)
{
    int count = 1;
    /* set LED0 pin mode to output */
    rt_pin_mode(LED0_PIN, PIN_MODE_OUTPUT);
//    clock_information();
    //获取网卡对象
    struct netdev* net = netdev_get_by_name("esp0");

    //阻塞判断当前网络是否正常连接
    while(netdev_is_internet_up(net) != 1)
    {
       rt_thread_mdelay(200);
    }
    //提示当前网络已就绪
    rt_kprintf("network is ok!\n");
    //NTP自动对时
    time_t cur_time;
    cur_time = ntp_sync_to_rtc(NULL);
    if (cur_time)
    {
        rt_kprintf("Cur Time: %s", ctime((const time_t*) &cur_time));
    }
    else
    {
        rt_kprintf("NTP sync fail.\n");
    }
    mount_init();
    while (count++)
    {
//        LOG_D("Hello RT-Thread!");
//        rt_thread_mdelay(1000);

        rt_pin_write(LED0_PIN, PIN_HIGH);
        rt_thread_mdelay(500);
        rt_pin_write(LED0_PIN, PIN_LOW);
        rt_thread_mdelay(500);
    }

    return RT_EOK;
}



//int oledRtc_test(void)
//{
//    rt_thread_t tid = rt_thread_create("rtlcd", oled_display, NULL, 1024, 11, 11);
//    rt_thread_startup(tid);
//
//    return RT_EOK;
//}
//INIT_APP_EXPORT(oledRtc_test);
